package com.vast.admin.service.impl;

import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vast.admin.entity.DealRecord;
import com.vast.common.core.domain.entity.SysUser;
import com.vast.admin.entity.enums.OperatorEum;
import com.vast.admin.entity.enums.QueryOrderEnum;
import com.vast.admin.mapper.DealRecordMapper;
import com.vast.admin.utils.QueryBuilder;
import com.vast.common.utils.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
@Service
public class DealRecordService extends ServiceImpl<DealRecordMapper, DealRecord> {


    public IPage<DealRecord> queryList(Page<DealRecord> page, boolean listMode, String queryItems, String orderField, QueryOrderEnum order) {
        String companyName = "";
        if (listMode) {
            // size 小于 0 不在查询 total 及分页，自动调整为列表模式。
            // 注意！！这个地方自己控制好！！
            page.setSize(-1);
        }
        if (!StringUtils.isEmpty(queryItems)) {
            List<QueryBuilder> queryBuilders = new ArrayList<>();
            String[] queryItemArr = queryItems.split(";");
            if(queryItemArr.length > 0){
                for (String queryItemStr : queryItemArr) {
                    String[] itemArr = queryItemStr.split(",");
                    if(itemArr.length == 3){
                        String as = itemArr[1];
                        String params = itemArr[2];
                        if(!StringUtils.isEmpty(params)){
                            queryBuilders.add(new QueryBuilder(itemArr[0], OperatorEum.valueOf(as),itemArr[2]));
                        }
                    }
                }
            }

            if(!queryBuilders.isEmpty()){
                companyName = queryBuilders.get(0).getParams();
            }

        }
        return baseMapper.queryList(page, companyName);
    }

    public boolean addOrUpdate(SysUser manager, DealRecord bean) {
        if (bean.getId() == null) {
            bean.setCreateBy(manager.getUserId());
            bean.setUpdateBy(manager.getUserId());
            bean.setCreateTime(DateTime.now());
            bean.setUpdateTime(DateTime.now());
            bean.setValid(1);
            //添加角色
            baseMapper.insert(bean);

            return true;
        } else {
            bean.setUpdateBy(manager.getUserId());
            bean.setUpdateTime(DateTime.now());
            bean.setValid(1);
            baseMapper.updateById(bean);
//
            return true;
//            return roleMenuService.saveBatch(roleMenus);
        }
    }

    public DealRecord info(SysUser manager, Long id) {
        DealRecord bean = baseMapper.selectById(id);
        return bean;
    }


    public boolean delete(SysUser manager, Long[] ids) {

        List<Long> idsList = java.util.Arrays.asList(ids);
        int count = baseMapper.deleteBatchIds(idsList);

        return count == ids.length;

    }


    public IPage<DealRecord> queryDealRecordList(Page<DealRecord> page, SysUser manager, DealRecord dealRecord) {
        return baseMapper.queryDealRecordList(page,manager, dealRecord);
    }
}
